package com.mobileares.midp.widgets.client.textbox2;

import com.google.gwt.user.client.ui.Widget;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-11-24
 * Time: 9:30:55
 * To change this template use File | Settings | File Templates.
 */
public class LabelValidatorFactory {
    public static void action(Widget widget, boolean flag, String mess) {
        BaseLabelText box = (BaseLabelText) widget;
        if (flag) {
             box.removeErrorMessage();
        } else {
          box.showErrorMessage(mess);
        }
    }

    public static ILabelValidator require() {
        return new ILabelValidator() {
            public boolean fire(Widget ui) {
                BaseLabelText box = (BaseLabelText) ui;
                boolean flag = true;
                if (box.getValue() == null || box.getValue().toString().equals("")) {
                    flag = false;
                }
                LabelValidatorFactory.action(box, flag, "必须填写");
                return flag;
            }
        };

    }

    public static ILabelValidator isFloat() {
        return new ILabelValidator() {
            public boolean fire(Widget ui) {
                TextBoxLabel box = (TextBoxLabel) ui;
                boolean flag = true;
                if (box.getValue() != null) {
                    flag = checkRegularExpression(box.getValue().toString(), "float");
                }
                LabelValidatorFactory.action(box, flag, "必须为数字格式");
                return flag;
            }
        };

    }

    public static ILabelValidator isMoreNum(final int num) {
        return isMoreNum(num, false);
    }
    public static ILabelValidator isMoreNum(final int num, final boolean eq) {
        return isMoreNum((float)num,eq);
    }
    public static ILabelValidator isMoreNum(final float num, final boolean eq) {
        return new ILabelValidator() {
            public boolean fire(Widget ui) {
                TextBoxLabel box = (TextBoxLabel) ui;
                boolean flag = true;
                if (box.getValue() != null) {
                    try {
                        if (!eq) {
                            if (Float.valueOf((String) box.getValue()) <= num) {
                                flag = false;
                            }
                        } else {
                            if (Float.valueOf((String) box.getValue()) < num) {
                                flag = false;
                            }
                        }

                    } catch (NumberFormatException ex) {
                        flag = false;
                    }
                }
                String des;
                if (eq)
                    des = "必须大于或者等于";
                else
                    des = "必须大于";
                LabelValidatorFactory.action(box, flag, "必须大于" + num);
                return flag;
            }
        };

    }

    public static ILabelValidator isLessNum(final int num) {
        return isLessNum(num, false);
    }
     public static ILabelValidator isLessNum(final int num, final boolean eq) {
      return   isLessNum((float)num,eq);
     }
    public static ILabelValidator isLessNum(final float num, final boolean eq) {
        return new ILabelValidator() {
            public boolean fire(Widget ui) {
                TextBoxLabel box = (TextBoxLabel) ui;
                boolean flag = true;
                if (box.getValue() != null) {
                    try {
                        if (!eq) {
                            if (Float.valueOf((String) box.getValue()) >= num) {
                                flag = false;
                            }
                        } else {
                            if (Float.valueOf((String) box.getValue()) > num) {
                                flag = false;
                            }
                        }
                    } catch (NumberFormatException ex) {
                        flag = false;
                    }
                }
                String des;
                if (eq)
                    des = "必须小于或者等于";
                else
                    des = "必须小于";
                LabelValidatorFactory.action(box, flag, des + num);
                return flag;
            }
        };

    }

    public static ILabelValidator isWord() {
        return new ILabelValidator() {

            public boolean fire(Widget ui) {
                TextBoxLabel box = (TextBoxLabel) ui;
                boolean flag = true;
                if (box.getValue() == null)
                    flag = true;
                else
                    flag = checkRegularExpression(box.getValue().toString(), "word");
                LabelValidatorFactory.action(box, flag, "只能输入3-20个以字母开头、可带数字、“_”、“.”的字串");
                return flag;

            }
        };
    }

    public static ILabelValidator isNumber() {
        return new ILabelValidator() {

            public boolean fire(Widget ui) {
                TextBoxLabel box = (TextBoxLabel) ui;
                boolean flag = true;
                if (box.getValue() == null)
                    flag = true;
                else
                    flag = checkRegularExpression(box.getValue().toString(), "number");
                LabelValidatorFactory.action(box, flag, "只能输入整数");
                return flag;

            }
        };
    }

    public static ILabelValidator isEmail() {
        return new ILabelValidator() {

            public boolean fire(Widget ui) {
                TextBoxLabel box = (TextBoxLabel) ui;
                boolean flag = true;
                if (box.getValue() == null)
                    flag = true;
                else
                    flag = checkRegularExpression(box.getValue().toString(), "email");
                LabelValidatorFactory.action(box, flag, "输入正确的邮箱格式");
                return flag;

            }
        };
    }

    public static ILabelValidator isPassword() {
        return new ILabelValidator() {

            public boolean fire(Widget ui) {
                TextBoxLabel box = (TextBoxLabel) ui;
                boolean flag = true;
                if (box.getValue() == null)
                    flag = true;
                else
                    flag = checkRegularExpression(box.getValue().toString(), "password");
                LabelValidatorFactory.action(box, flag, "只能输入6-20个字母、数字、下划线");
                return flag;

            }
        };
    }

    public static ILabelValidator isTelephone() {
        return new ILabelValidator() {

            public boolean fire(Widget ui) {
                TextBoxLabel box = (TextBoxLabel) ui;
                boolean flag = true;
                if (box.getValue() == null)
                    flag = true;
                else
                    flag = checkRegularExpression(box.getValue().toString(), "mobile");
                LabelValidatorFactory.action(box, flag, "填写正确电话号码");
                return flag;

            }
        };
    }

    public static ILabelValidator isIP() {
        return new ILabelValidator() {

            public boolean fire(Widget ui) {
                TextBoxLabel box = (TextBoxLabel) ui;
                boolean flag = true;
                if (box.getValue() == null)
                    flag = true;
                else
                    flag = checkRegularExpression(box.getValue().toString(), "ip");
                LabelValidatorFactory.action(box, flag, "填写正确IP地址");
                return flag;

            }
        };
    }


    public static native boolean checkRegularExpression(String value, String exp)/*-{
              var __RegExp_Word = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){2,19}$/;
              var __RegExp_Chinese =/[^\x20-\x80]+/;
              var __RegExp_Number = /^[\d]+$/;
              var __RegExp_Float = /^[\d]+[\.]?[\d]{0,10}$/;
              var __RegExp_Email = /[-\.\w]+@[-\.\w]+\.\w{1,3}/;
              var __RegExp_Date = /\d{2,4}-\d{1,2}-\d{1,2}|\d{2,4}\/\d{1,2}\/\d{1,2}|\d{2,4}\.\d{1,2}\.\d{1,2}/;
              var __RegExp_Time =/\d{2}:\d{2}:\d{2}/;
              var __RegExp_DateTime =  /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/;
              var __RegExp_Password =  /^(\w){6,20}$/;
              var __RegExp_Mobile = /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
              var __RegExp_IP = /^([1-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){2}([1-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/;
             var validationExpression;
             switch(exp.toLowerCase()) {
            case "ip":
               validationExpression=__RegExp_IP;
             break;
			case "word":
				validationExpression = __RegExp_Word;
				break;
			case "number":
				validationExpression = __RegExp_Number;
				break;
			case "float":
				validationExpression = __RegExp_Float;
				break;
			case "email":
				validationExpression = __RegExp_Email;
				break;
			case "date":
				validationExpression = __RegExp_Date;
				break;
			case "time":
				validationExpression = __RegExp_Time;
				break;
			case "datetime":
				validationExpression = __RegExp_DateTime;
				break;
			case "chinese":
				validationExpression = __RegExp_Chinese;
				break;
            case "password":
				validationExpression = __RegExp_Password;
				break;
            case "mobile":
				validationExpression = __RegExp_Mobile;
				break;
			default:
				alert(exp + "No RegExp match!");
				return false;
             }
             return (validationExpression.test(value));
     }-*/;
}
